/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.core.framework;

import com.openwaf.common.annotation.EnsureIdentifier;

/**
http://en.wikibooks.org/wiki/Java_Programming/Types
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html
 */
final public class TypeCast {

    static native boolean instanceOf(Object t, Object c)/*-{
    if(t==null || t==undefined){
    return false;
    }
    if(t._i && c.prototype && c.prototype._id){
    return c.prototype._id in t._i;
    }
    return t instanceof c;
    }-*/;
    //narrow_byte
    @EnsureIdentifier
    static native byte nb(double v)/*-{
    return v << 24 >> 24;
    }-*/;
    //narrow_char
    @EnsureIdentifier
    static native char nc(double v)/*-{
    return v & 0xFFFF;
    }-*/;
    //narrow_int
    @EnsureIdentifier
    static native int ni(double v)/*-{
    return ~~v;
    }-*/;
    //narrow_short
    @EnsureIdentifier
    static native short ns(double v)/*-{
    return v << 16 >> 16;
    }-*/;
    @EnsureIdentifier
    static byte rb(double x) {
        return nb(ri(x));
    }
    @EnsureIdentifier
    static char rc(double x) {
        return nc(ri(x));
    }
    @EnsureIdentifier
    static native int ri(double x) /*-{
    return ~~Math.max(Math.min(x, 2147483647), -2147483648);
    }-*/;
    @EnsureIdentifier
    static short rs(double x) {
        return ns(ri(x));
    }
    @EnsureIdentifier
    static native boolean isNull(Object o)/*-{
    return !o;
    }-*/;
}
